package com.ftg.learn.chapter09.collection;

import com.ftg.learn.chapter04.Dog;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * set学习
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/15 10:11
 */

public class LearnSet {
    public static void main(String[] args) {

        HashSet<Integer> set = new HashSet<>(5);
        System.out.println(set.size());
        set.add(23);
        System.out.println(set.size());
        set.add(23);
        System.out.println("去掉重复元素，重复元素不输出");
        System.out.println(set.size());

        HashSet<Double> set1 = new HashSet<>(3);
        set1.add(Double.valueOf(3));
        set1.add(3.0);
        set1.add(3.00);
        System.out.println(set1);

        HashSet<Dog> set2 = new HashSet<>(2);

        // 判断两个对象是否同  equals    hashcode()
        Dog d1=new Dog();
        d1.setName("apc");
        Dog d2=new Dog();
        d2.setName("7ef");
        set2.add(d1);
        set2.add(d2);
        System.out.println("1-->"+d1.hashCode());
        System.out.println("2-->"+d2.hashCode());
        System.out.println(d1.equals(d2));

        System.out.println(set2);

        //Dog里调用了Data,重写了equals方法和tostring方法，没赋值时hash值一样 只能存一个
        Dog d3=new Dog();
        Dog d4=new Dog();
        set2.add(d3);
        set2.add(d4);
        System.out.println("3-->"+d3.hashCode());
        System.out.println("4-->"+d4.hashCode());
        System.out.println(d3.equals(d4));
        System.out.println(set2);

        List<Dog> list1 = new ArrayList<>();
        Dog d=new Dog();
        list1.add(d);
        list1.add(d);
        System.out.println(d);

        //输入不同字符可能会出现hash值相同的情况，产生hash冲突
        System.out.println("Aa".hashCode());
        System.out.println("BB".hashCode());
    }
}
